package com.night.card.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DelConfig
 * @Description: RabbitMqConfiguration配置
 * @author: CHX42
 * @date: 2022/9/23-11:44
 * @version: v1.0
 */
@Configuration
public class RabbitMqConfiguration {
    //死信队列，实现延迟效果
    //一组（交换机和队列），队列没有消费者，就是让它变成死信
    //声明交换机
    @Bean
    public TopicExchange inMoneyExchange() {
        return new TopicExchange("inMoneyExchange");
    }

    //声明队列
    @Bean
    public Queue inMoneyOrder() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        // @Nullable Map<String, Object> arguments  队列属性
        Map<String, Object> arguments = new HashMap<>();
//        arguments.put("x-message-ttl", 20000);                          //等待超时间时间
        arguments.put("x-dead-letter-exchange", "inMoneyExchangeDlx");    //超时绑定交换机
        arguments.put("x-dead-letter-routing-key", "falseInMoney");         //交换机路由
        return new Queue("inMoneyOrder", true, false, false, arguments);
    }
    //交换机绑定队列的路由
    @Bean
    public Binding bindingOrderDelay(Queue inMoneyOrder, TopicExchange inMoneyExchange) {
        return BindingBuilder.bind(inMoneyOrder).to(inMoneyExchange).with("#.innMoney.#");
    }

    //定义死信交换机和死信队列
    @Bean
    public TopicExchange inMoneyExchangeDlx() {
        return new TopicExchange("inMoneyExchangeDlx");
    }

    //声死信队列
    @Bean
    public Queue inMoneyOrderDlx() {
        return new Queue("inMoneyOrderDlx");
    }

    //绑定路由到队列(路由通配)
    @Bean
    public Binding bindingOrderDlx(Queue inMoneyOrderDlx, TopicExchange inMoneyExchangeDlx) {
        return BindingBuilder.bind(inMoneyOrderDlx).to(inMoneyExchangeDlx).with("#.falseInMoney.#");
    }
    //
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
